package com.bird.controller;

import com.bird.controller.frontend.MainPageController;
import com.bird.controller.superadmin.HeadLineOperationController;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: sijunqiang
 * @Date: 2020/3/29 19:48
 * @Description: 该类的功能是：1：拦截所有的请求 2：解析所有的请求 3：转发请求的路径到controller层的方法中
 * <p>
 * "/" 拦截所有的请求 不包括.jsp
 * "/*" 拦截所有的请求 包括.jsp 请求
 */
@WebServlet("/")
public class DispatcherServlet extends HttpServlet {

    private static final String FRONTED_GET_MAIN_PAGE_INFO = "/frontend/getmainpageinfo";
    private static final String SUPER_ADMIN_ADD_HEAD_LINE = "/superadmin/addheadline";
    private static final String GET = "GET";
    private static final String POST = "POST";


    @Override
    public void init() {
        System.out.println("项目启动时，当第一次访问时");
    }


    /**
     * 重写父类里面当当sevice方法
     *
     * @param request
     * @param response
     */
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) {
        //获取请求的路径
        System.out.println(request.getServletPath());
        //获取请求的方法
        System.out.println(request.getMethod());

        //对请求的分发
        if (FRONTED_GET_MAIN_PAGE_INFO.equals(request.getServletPath()) && GET.equals(request.getMethod())) {
            //获取请求头beanId
            String beanId ="1234";
            ThreadLocal<Object> threadLocal = new ThreadLocal<>();
            threadLocal.set(beanId);
            new MainPageController().getMainPageInfo(request, response);
        } else if (SUPER_ADMIN_ADD_HEAD_LINE.equals(request.getServletPath()) && POST.equals(request.getMethod())) {
            new HeadLineOperationController().addHeadLine(request, response);
        }

    }


}
